﻿<%@ WebHandler Language="C#" Class="workReportSend" %>

using System;
using System.Web;
using Chenxin.Lib.Utility;

public class workReportSend : AjaxHandlerBase {

    public workReportSend()
    {
        //发送工作报告
        htTypePermit.Add("save", Constants.Permit_SendWorkReport);
        //上传文件
        htTypePermit.Add("fileupload", Constants.Permit_SendWorkReport);
        //删除工作报告
        htTypePermit.Add("del", Constants.Permit_DelWorkReport);
    }
    
    override public void RequestProcess(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        string strRet = "";
        string strOp = context.Request.Params["op"];
        try
        {
            switch (strOp)
            {
                case "getOrgTree":
                    strRet = getOrgTree(context);
                    break;
                case "getRoleTree":
                    strRet = getRoleTree(context);
                    break;
                case "fileupload":
                    Fileupload(context, Constants.FilePath_WorkReport);
                    return;
                    break;
                case "save":
                    strRet = sendWorkReport(context);
                    break;
                case "del":
                    strRet = delWorkReport(context);
                    break;
            }
            context.Response.Write(strRet);
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            context.Response.Write(strRet);
        }
    }

    //获得组织用户树
    private string getOrgTree(HttpContext context)
    {
        string strRet = string.Empty;
        
        Guid gdParentId = Guid.Empty;
        Guid gdGrandfatherId = Guid.Empty;
        
        if (!string.IsNullOrEmpty(context.Request.Params["id"]))
        {
            string strId = context.Request.Params["id"];
            gdParentId = new Guid(strId.Substring(4));
        }
        else
        {
            //所有人权限
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_SendReportAll))
            {
            }
            //城市权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_SendReportCity))
            {
                gdGrandfatherId = new Guid("00000000-0000-0000-0000-000000000001");
                gdParentId = CurrentUserInfo.CityId;
            }
            //公司权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_SendReportCompany))
            {
                gdGrandfatherId = CurrentUserInfo.CityId;
                gdParentId = CurrentUserInfo.CompanyId;
            }
            else
            {
                gdGrandfatherId = CurrentUserInfo.CityId;
                gdParentId = CurrentUserInfo.CompanyId;
            }
        }
        Chenxin.Lib.Hr.BLL.WorkReport bllWrkRpt = new Chenxin.Lib.Hr.BLL.WorkReport();

        System.Data.DataSet ds = bllWrkRpt.getOgrMembersByParentId(gdParentId, gdGrandfatherId);

        strRet = retExecResult(Constants.ExecResult.Succ, "", crtHtml(ds,"Org"));
        
        return strRet;
    }
    
    //获得角色用户数据
    private string getRoleTree(HttpContext context)
    {
        string strRet;
        Guid gdParentId = Guid.Empty;
        Guid gdGrandfatherId = Guid.Empty;
        
        if (!string.IsNullOrEmpty(context.Request.Params["id"]))
        {
            string strId = context.Request.Params["id"];
            gdParentId = new Guid(strId.Substring(5));
        }
        else
        {
            //所有人权限
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_SendReportAll))
            {
            }
            //城市权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_SendReportCity))
            {
                gdGrandfatherId = new Guid("00000000-0000-0000-0000-000000000001");
                gdParentId = CurrentUserInfo.CityId;
            }
            //公司权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_SendReportCompany))
            {
                gdGrandfatherId = CurrentUserInfo.CityId;
                gdParentId = CurrentUserInfo.CompanyId;
            }
            else
            {
                gdGrandfatherId = CurrentUserInfo.CityId;
                gdParentId = CurrentUserInfo.CompanyId;
            }
        }
        Chenxin.Lib.Hr.BLL.WorkReport bllWrkRpt = new Chenxin.Lib.Hr.BLL.WorkReport();

        System.Data.DataSet ds = bllWrkRpt.getRoleMembersByParentId(gdParentId, gdGrandfatherId);

        strRet = retExecResult(Constants.ExecResult.Succ, "", crtHtml(ds,"Role"));

        return strRet; 
    }
    
    //生成响应字符串
    private string crtHtml(System.Data.DataSet ds,string Type)
    {
        System.Text.StringBuilder strRet = new System.Text.StringBuilder();
        strRet.Append("[");

        for (int idxRow = 0; idxRow < ds.Tables[0].Rows.Count; idxRow++)
        {
            strRet.AppendFormat("{{id:\"" + Type + "_{0}\",text:\"{1}\"}},", ds.Tables[0].Rows[idxRow]["NodeId"], ds.Tables[0].Rows[idxRow]["NodeName"]);
        }
        if (strRet.Length > 1)
        {
            strRet.Remove(strRet.Length - 1, 1);
        }
        strRet.Append("]");
        
        return strRet.ToString();
    }

    //发送工作报告
    private string sendWorkReport(HttpContext context)
    {
        string strRet;
        string strMsg;

        string strModel = context.Request.Params["model"];
        string strFiles = context.Request.Params["filenames"];
        string strSaveFiles = context.Request.Params["savenames"];

        string strPath = Constants.FilePath_WorkReport;
        
        Chenxin.Lib.Hr.Entity.WorkReport_OutBox entOutBox =
        (Chenxin.Lib.Hr.Entity.WorkReport_OutBox)Chenxin.Lib.Utility.JsonHelp.Convert2Entity(strModel, typeof(Chenxin.Lib.Hr.Entity.WorkReport_OutBox));

        entOutBox.isDeleted = false;
        entOutBox.SenderId = CurrentUserInfo.UserId;
        entOutBox.SenderName = CurrentUserInfo.UserName;
        entOutBox.SenderCityId = CurrentUserInfo.CityId;
        entOutBox.SenderCityName = CurrentUserInfo.CityName;
        entOutBox.SenderCode = CurrentUserInfo.UserCode;
        entOutBox.SenderCompanyId = CurrentUserInfo.CompanyId;
        entOutBox.SenderCompanyName = CurrentUserInfo.CompanyName;
        entOutBox.SenderDepartId = CurrentUserInfo.DepartId;
        entOutBox.SenderDepartName = CurrentUserInfo.DepartName;
        entOutBox.SendTime = DateTime.Now;

        //添加
        if (entOutBox.RecordId.Equals(Guid.Empty))
        {
            entOutBox.RecordId = Guid.NewGuid();
            //发件箱
            Chenxin.Lib.Hr.BLL.WorkReport_OutBox.Add(entOutBox);
            //收件箱
            addWorkReportInBox(entOutBox);
            //附件
            if (!string.IsNullOrEmpty(strFiles))
            {
                strPath = strPath + strSaveFiles;
                AddAttachments(entOutBox.RecordId, strPath, strFiles, 10);
            }

            strMsg = "发送成功";
        }
        //编辑
        else
        {
            string uploadfl = context.Request.Params["uploadfl"];

            Chenxin.Lib.Hr.Entity.WorkReport_OutBox entOldOutBox = Chenxin.Lib.Hr.BLL.WorkReport_OutBox.GetModel(entOutBox.RecordId);

            entOutBox.ParentId = entOldOutBox.ParentId;
            entOutBox.RootId = entOldOutBox.RootId;
            
            //发件箱
            Chenxin.Lib.Hr.BLL.WorkReport_OutBox.Update(entOutBox);
            //更新回复表
            Chenxin.Lib.Hr.Entity.WorkReport_Reply entReply = Chenxin.Lib.Hr.BLL.WorkReport_Reply.GetModel(entOutBox.RecordId);

            //回复
            if (entReply != null)
            {
                entReply.CityId = entOutBox.SenderCityId;
                entReply.CityName = entOutBox.SenderCityName;
                entReply.CompanyId = entOutBox.SenderCompanyId;
                entReply.CompanyName = entOutBox.SenderCompanyName;
                entReply.DepartId = entOutBox.SenderDepartId;
                entReply.DepartName = entOutBox.SenderDepartName;
                entReply.UserId = entOutBox.SenderId;
                entReply.UserCode = entOutBox.SenderCode;
                entReply.UserName = entOutBox.SenderName;
                entReply.SendTime = entOutBox.SendTime;
                entReply.Subject = entOutBox.Subject;
                entReply.Content = entOutBox.Content;

                Chenxin.Lib.Hr.BLL.WorkReport_Reply.Update(entReply);
            }
            
            //更新收件箱状态
            System.Collections.Generic.List<  Chenxin.Lib.Hr.Entity.WorkReport_InBox> lsInBox =  Chenxin.Lib.Hr.BLL.WorkReport_InBox.GetListByReportId(entOutBox.RecordId);

            for (int idxCnt = 0; idxCnt < lsInBox.Count; idxCnt++)
            {
                lsInBox[idxCnt].isReaded = false;
                Chenxin.Lib.Hr.BLL.WorkReport_InBox.Update(lsInBox[idxCnt]);
            }

            //删除原有文件
            if (!string.IsNullOrEmpty(uploadfl))
            {
                System.Collections.Generic.List<Chenxin.Lib.Hr.Entity.Attachment> lsAtt = Chenxin.Lib.Hr.BLL.Attachment.GeListByRelationId(entOutBox.RecordId);

                for (int idxCnt = 0; idxCnt < lsAtt.Count; idxCnt++)
                {
                    DelAttachments(context, lsAtt[idxCnt].RecordId);
                }
                //DelAttachments(context, new Guid(uploadfl));
            }
            //附件
            if (!string.IsNullOrEmpty(strFiles))
            {
                strPath = strPath + strSaveFiles;
                AddAttachments(entOutBox.RecordId, strPath, strFiles, 10);
                //回复类型的工作报告
                if (entReply != null)
                {
                    AddAttachments(entOutBox.RecordId, strPath, strFiles, 20);
                }
            }

            Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HR);
            string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.HR_WORKREPORT, entOutBox.RecordId.ToString());
            //删除缓存列表
            cs.Delete(strCacheKey);
            
            strMsg = "修改成功";
        }
        strRet = retExecResult(Constants.ExecResult.Succ, strMsg, "");
        return strRet;
    }
    
    //添加收件箱
    private void addWorkReportInBox(Chenxin.Lib.Hr.Entity.WorkReport_OutBox entOutBox)
    {
        Chenxin.Lib.Hr.BLL.WorkReport bllWrkRpt = new Chenxin.Lib.Hr.BLL.WorkReport();
        Chenxin.Lib.Hr.Entity.WorkReport_InBox entInBox;
        System.Data.DataSet ds = bllWrkRpt.getReceiversInfo(entOutBox.ReceiverId);

        for (int idxRow = 0; idxRow < ds.Tables[0].Rows.Count; idxRow++)
        {
            entInBox = new Chenxin.Lib.Hr.Entity.WorkReport_InBox();
            entInBox.RecordId = Guid.NewGuid();
            entInBox.WorkReportId = entOutBox.RecordId;
            entInBox.isDeleted = false;
            entInBox.isReaded = false;

            entInBox.ReceiverCityId = new Guid(ds.Tables[0].Rows[idxRow]["BelongCityId"].ToString());
            entInBox.ReceiverCityName = ds.Tables[0].Rows[idxRow]["BelongCityName"].ToString();
            entInBox.ReceiverCompanyId = new Guid(ds.Tables[0].Rows[idxRow]["BelongCompanyId"].ToString());
            entInBox.ReceiverCompanyName = ds.Tables[0].Rows[idxRow]["BelongCompanyName"].ToString();
            entInBox.ReceiverDepartId = new Guid(ds.Tables[0].Rows[idxRow]["BelongDepartId"].ToString());
            entInBox.ReceiverDepartName = ds.Tables[0].Rows[idxRow]["BelongDepart"].ToString();
            entInBox.ReceiverId = new Guid(ds.Tables[0].Rows[idxRow]["UserId"].ToString());
            entInBox.ReceiverName = ds.Tables[0].Rows[idxRow]["UserName"].ToString();

            Chenxin.Lib.Hr.BLL.WorkReport_InBox.Add(entInBox);
        }
    }

    //删除工作报告
    private string delWorkReport(HttpContext context)
    {
        string strRet;
        string RecordId = context.Request.Params["RecordId"];

        //删除发件箱
        Chenxin.Lib.Hr.BLL.WorkReport_OutBox.Delete(new Guid(RecordId));
        //删除收件箱
        Chenxin.Lib.Hr.BLL.WorkReport_InBox.DeleteByReportId(new Guid(RecordId));
        //删除附件
        System.Data.DataSet ds = Chenxin.Lib.Hr.BLL.Attachment.GetListByBelong(RecordId, 10);
        if (ds.Tables[0].Rows.Count > 0)
        {
            DelAttachments(context, new Guid(ds.Tables[0].Rows[0]["RecordId"].ToString()));
        }
        //删除附件表
        Chenxin.Lib.Hr.BLL.Attachment.DeleteByReportId(new Guid(RecordId));
        
        //删除回复
        Chenxin.Lib.Hr.BLL.WorkReport_Reply.DeleteByReportId(new Guid(RecordId));
        
        strRet = retExecResult(Constants.ExecResult.Succ, "删除成功", "");
        return strRet;
    }
}